<template>
  <div>
    <Card ref="card">
      <p slot="title">{{titlename}}</p>
      <a href="#" slot="extra" @click.prevent="refData">刷新</a>
      <Row :gutter="16">
        <Col span="6">
          <Input v-model="str" size="large" placeholder="请输入项目名称" />
        </Col>

        <Col span="4">
          <Button @click="Search" size="large" type="primary" icon="ios-search">查询</Button>
        </Col>
      </Row>
    </Card>
    <Row class="margin-top-10">
      <Table border :columns="tableTitle" :data="tableData" ref="table" :height="tableHeight"></Table>
    </Row>
    <Row class="margin-top-10">
      <Page
        :total="total"
        :current="pPage"
        :page-size="pSize"
        :page-size-opts="[50,100,300,500,1000,2000,5000]"
        @on-change="s=>{pPage=s;Search();}"
        @on-page-size-change="p=>{pSize=p;pPage=1;Search()}"
        show-total
        show-sizer
      />
    </Row>
    <Modal :mask-closable="false" v-model="modal2" width="500">
      <p slot="header" class="color-primary text-align-center">
        <Icon type="ios-information-circle"></Icon>
        <span>存档评估结果</span>
      </p>

      <br />

      <Row>
        <Select filterable label-in-value v-model="areaTmp" size="large" placeholder="请选择存档地区">
          <Option v-for="item in areaList" :value="item.Gid" :key="item.Gid">{{ item.AreaName }}</Option>
        </Select>
      </Row>
      <br />

      <div slot="footer">
        <Button type="primary" size="large" v-on:click="Save">保存</Button>
      </div>
    </Modal>
  </div>
</template>
<script>
import dayjs from "dayjs";
import { Message, Modal } from "view-design";
import { SearchArea } from "@/api/data";
import {
  SearchDBGrouphasPage,
  DeleteDBGroup,
  SavedDBGroup,
} from "@/api/dbdata";
export default {
  name: "db_search_group",
  data() {
    return {
      str: "",
      total: 0,
      pPage: 1,
      pSize: 50,
      tableHeight: 450,
      modal2: false,
      areaList: [],
      areaTmp: "",
      tmpGid: "",
      tableTitle: [
        {
          title: "序号",
          key: "R_INDEX",
          align: "center",
          width: 80,
        },
        {
          title: "项目名称",
          key: "GroupName",
          minWidth: 400,
          render: (h, params) => {
            return h(
              "a",
              {
                props: {
                  href: "#",
                },
                style: {
                  fontSize: "18px",
                },
                on: {
                  click: () => {
                    this.turntodetail(params.row);
                  },
                },
              },
              params.row.GroupName
            );
          },
        },
        {
          title: "实施日期",
          key: "sys_CreateDate",
          align: "center",
          render: (h, params) => {
            return h(
              "div",
              dayjs(params.row.sys_CreateDate).format("YYYY年MM月DD日")
            );
          },
        },
        {
          title: "操作",
          key: "action",
          align: "center",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small",
                  },
                  style: {
                    marginRight: "5px",
                  },
                  on: {
                    click: () => {
                      this.tmpGid = params.row.Gid;
                      this.modal2 = true;
                    },
                  },
                },
                "存档"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "error",
                    size: "small",
                  },
                  on: {
                    click: () => {
                      this.Delete(params.row.Gid);
                    },
                  },
                },
                "删除"
              ),
            ]);
          },
        },
      ],
      tableData: [],
      titlename: "",
      routername: "",
    };
  },

  methods: {
    SearchArea() {
      let vm = this;
      SearchArea().then((res) => {
        const data = res.data;
        if (data.Type == 1) {
          vm.areaList = data.Data;
        } else {
          Message.error(data.Message);
        }
      });
    },

    Search() {
      let vm = this;
      var reqData = {
        str: vm.str,
        issaved: 0,
        page: vm.pPage,
        size: vm.pSize,
      };
      SearchDBGrouphasPage(reqData).then((res) => {
        const data = res.data;
        if (data.Type == 1) {
          vm.total = data.Data.Total;
          vm.tableData = data.Data.Rows;
        } else {
          vm.$Message.error(data.Message);
        }
      });
    },
    refData() {
      this.SearchArea();
      this.Search();
    },
    Save() {
      let vm = this;
      if (!this.tmpGid) {
        Message.error("存档分组不能为空");
        return;
      }
      if (!this.areaTmp) {
        Message.error("地区不能为空");
        return;
      }
      var reqData = {
        gid: this.tmpGid,
        areaid: this.areaTmp,
      };
      SavedDBGroup(reqData).then((res) => {
        const data = res.data;
        if (data.Type == 1) {
          this.Search();
          this.$Modal.success({ title: "提示", content: "存档成功" });
          this.modal2 = false;
        } else {
          this.$Modal.error({ title: "提示", content: data.Message });
        }
      });
    },
    Delete(id) {
      Modal.confirm({
        title: "提示",
        content: "信息删除后无法恢复，确定要删除吗？",
        onOk: () => {
          DeleteDBGroup(id).then((res) => {
            const data = res.data;
            if (data.Type == 1) {
              this.Search();
              this.$Modal.success({ title: "提示", content: "删除成功" });
            } else {
              this.$Modal.error({ title: "提示", content: data.Message });
            }
          });
        },
        onCancel: () => {},
      });
    },
    turntodetail(item) {
      this.$router.push({
        name: "db_search_person",
        params: { id: item.Gid },
        query: { groupname: item.GroupName },
      });
    },
  },
  created() {},
  mounted() {
    var cardHeight = this.$refs.card.$el.offsetHeight;
    this.tableHeight = window.innerHeight - cardHeight - 200;
    this.titlename = this.$route.meta.title;
    this.routername = this.$route.name;
    this.refData();
  },
};
</script>
